import { router } from "./router";
import { useUserStore } from "./store/modules/user";

const whiteList = ["/login", "/register", '/admin/order-list'];

const redirectHome = "/order/add";

router.beforeEach(async (to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  const { getToken, checkUserIsLogined, getIsLogined  } = useUserStore();

  if (getToken || getIsLogined) {
    if (whiteList.includes(to.path)) {
      next(redirectHome);
    }
    next();
  } else {
    // 没有token查询是否登录
    if (await checkUserIsLogined()) {
      next()
    }

    if (whiteList.includes(to.path)) {
      next();
    } else {
      next({ path: "/login" });
    }
  }
});
